package com.cskaoyan.service.impl;

import com.cskaoyan.bean.Categoryl1Data;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.model.MarketCategory;
import com.cskaoyan.model.MarketCategoryExample;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    @Override
    public List<MarketCategory> list() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andPidEqualTo(0).andDeletedEqualTo(false);
        List<MarketCategory> parents = categoryMapper.selectByExample(example);
        for (MarketCategory category : parents) {
            example.clear();
            example.createCriteria().andPidEqualTo(category.getId()).andDeletedEqualTo(false);
            List<MarketCategory> childrenList = categoryMapper.selectByExample(example);
            category.setChildren(childrenList);
        }
        sqlSession.commit();
        sqlSession.close();
        return parents;
    }

    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        categoryMapper.insertSelective(marketCategory);
        sqlSession.commit();
        sqlSession.close();
        return marketCategory;
    }

    @Override
    public void update(MarketCategory marketCategory) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        marketCategory.setUpdateTime(new Date());
        categoryMapper.updateByPrimaryKeySelective(marketCategory);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(MarketCategory marketCategory) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        marketCategory.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(marketCategory); //逻辑删除
//        categoryMapper.deleteByPrimaryKey(marketCategory.getId()); //物理删除
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Categoryl1Data> l1() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andPidEqualTo(0);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(example);
        List<Categoryl1Data> dataList = new ArrayList<>();
        for (MarketCategory category : categoryList) {
            Categoryl1Data data = new Categoryl1Data(category.getId(), category.getName());
            dataList.add(data);
        }
        sqlSession.commit();
        sqlSession.close();
        return dataList;
    }
}
